package es.ats.codington.web.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.services.EventService;

/**
 * Servlet implementation class EventUpdateServlet
 */
@WebServlet("/EventUpdateServlet")
public class EventUpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EventUpdateServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		RequestDispatcher rd=request.getRequestDispatcher("NewCodington_eventUpdate.jsp");  
		rd.forward(request,response); 
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html");  
		HttpSession session=request.getSession(true);
		
		try{
			
		Event event=new Event();
		EventService es=new EventService();
		
		event.setEventName(request.getParameter("oldeventname"));
		
  	
	    	if(es.checkEvent(event)){
	    		
	    		event.setIdEvent(es.getIdEvent(event));
	    		event=es.getEvent(event);
	    		
	    		if(request.getParameter("neweventname").equals("")==false)
	    	    {
	    			event.setEventName(request.getParameter("neweventname"));
	    	    }
	    		if(request.getParameter("newdescription").equals("")==false)
	    	    {
	    			event.setDescription(request.getParameter("newdescription"));
	    	    }
	    		if(request.getParameter("newduration").equals("")==false)
	    	    {
	    			event.setDuration(Integer.parseInt(request.getParameter("newduration")) );
	    	    }
	    		if(request.getParameter("neweventtype").equals("")==false)
	    	    {
	    			event.setEventType(request.getParameter("neweventtype"));
	    	    }
	    		if(request.getParameter("newtotaltickets").equals("")==false)
	    	    {
	    			event.setSeatsAvailable(Integer.parseInt(request.getParameter("newtotaltickets")));
	    	    }
	    		if(request.getParameter("newplace").equals("")==false)
	    	    {
	    			event.setPlace(request.getParameter("newplace"));
	    	    }
	    		
	    		
	    		
	    		if(es.updateEvent(event)==1){
	    			RequestDispatcher rd=request.getRequestDispatcher("NewCodington_myPortal.jsp");  
	    			rd.forward(request,response); 
	    			
	    			}
	    		else {
	    			session.setAttribute("error", "Event not updated");
		    		 
		    		RequestDispatcher rd=request.getRequestDispatcher("NewCodington_eventUpdate.jsp");  
		    		rd.include(request,response);
	    		
	    		}
	    	}
	    	else {
	    		session.setAttribute("error", "This event NOT exists");
	    		 
	    		RequestDispatcher rd=request.getRequestDispatcher("NewCodington_eventUpdate.jsp");  
	    		rd.include(request,response);
	    		
	    	}
				
	    }
	    catch(Exception eee){
	    	eee.getMessage();	
	    }
	}

}
